/*
 * RendererView.cpp
 *
 *  Created on: Sep 4, 2011
 *      Author: Patrick
 */

#include "MediaCenter.h"
#include "Renderer.h"
#include "Repository.h"
#include "Widget.h"
#include <math.h>

extern "C" {
#include "video/Video.h"
}

MCTextObject textObject;
MCComponent sceneComponent;

void drawMediaStatusBar(RendererContext* r);

void Renderer::process(double delta) {

	if (!textObject.texture) {

		MCSpriteComponent* fooComponent = new MCSpriteComponent();
		MCVideoComponent* videoComponent = new MCVideoComponent();

		textObject = MCTextCreate((char *)"Hello World", MCFontLoad(MC_FONT_TTF_DEFAULT_NORMAL), 1, 1, 1);

		sceneComponent.x = 0;
		sceneComponent.y = 0;
		sceneComponent.w = this->context.screenWidth;
		sceneComponent.h = this->context.screenHeight;

		videoComponent->setTexture(); // This will create the default (only) video texture
		videoComponent->x = 0;
		videoComponent->y = 0;
		videoComponent->w = this->context.screenWidth;
		videoComponent->h = this->context.screenHeight;

		fooComponent->setTexture((char*)"FooTex", (char*)"resources/foo.jpg");
		fooComponent->x = 50;
		fooComponent->y = 50;
		fooComponent->w /= 4.0;
		fooComponent->h /= 4.0;

		sceneComponent.addChild(videoComponent);
		sceneComponent.addChild(fooComponent);

	}

}

void Renderer::render() {

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	sceneComponent.draw();

	//MCTextRender(textObject, 50, 50);

	//	drawMediaStatusBar(&context);
	//
	//	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	//
	//	fooComponent.draw();
	//
	//	MCTextRender(textObject, 50, 50);


	SDL_GL_SwapBuffers();

}

void drawMediaStatusBar(RendererContext* ctx) {

	//	float mediaPosition = MCVideoGetPosition();
	//	float mediaDuration = MCVideoGetDuration();
	int w = ctx->screenWidth;
	int h = ctx->screenHeight;

	glDisable(GL_TEXTURE_2D);

	glBegin(GL_QUADS);

	// Backdrop
	glColor4f(0.0f, 0.0f, 0.0f, 0.9f);
	glVertex2i(0, h);
	glVertex2i(w, h);
	glVertex2i(w, h - 96);
	glVertex2i(0, h - 96);
	glVertex2i(0, h - 96);
	glVertex2i(w, h - 96);
	glColor4f(0.0f, 0.0f, 0.0f, 0.0f);
	glVertex2i(w, h * 0.5f);
	glVertex2i(0, h * 0.5f);

	// Empty status background
	glColor4f(0.01f, 0.01f, 0.01f, 1.0f);
	glVertex2i(w * 0.2, h - 96 + 96 * 0.7);
	glVertex2i(w * 0.8, h - 96 + 96 * 0.7);
	glColor4f(0.01f, 0.01f, 0.01f, 0.6f);
	glVertex2i(w * 0.8, h - 96 + 96 * 0.3);
	glVertex2i(w * 0.2, h - 96 + 96 * 0.3);

	glEnd();

	glEnable(GL_TEXTURE_2D);

}
